package com.cinema.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cinema.bean.Bill;
import com.cinema.bean.Role;
import com.cinema.bean.Session;
import com.cinema.bean.User;
import com.cinema.service.BillService;
import com.cinema.service.RoleService;
import com.cinema.service.SessionService;
import com.cinema.service.UserService;
import com.cinema.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("bill")
@CrossOrigin
public class BillController {
    @Autowired
    BillService billService;
    @Autowired
    UserService userService;
    @Autowired
    SessionService sessionService;
    @GetMapping("list")
    public Result getList(){
        List<Bill> list=billService.list();
        for (Bill item:list){
            User user=userService.getById(item.getUserId());
            item.setUser(user);
        }
        for(Bill item:list){
            Session session=sessionService.getById(item.getSessionId());
            item.setSession(session);
        }
        return Result.success(list);
    }

    @GetMapping("page")
    public Result getPage(Page page, @RequestParam("searchText") String searchText){
        System.out.println(searchText);
        IPage<User> result=null;
        if(searchText==null||searchText.trim().isEmpty()){
            result=billService.page(page);
        }else {
            QueryWrapper wrapper=new QueryWrapper();

            wrapper.like("bill_id",searchText);
            wrapper.or();
            wrapper.like("pay_state",searchText);
            wrapper.or();
            wrapper.like("seats",searchText);
            result=billService.page(page,wrapper);
        }
        return Result.success(result);
    }

    @PostMapping
    public Result add(@RequestBody Bill bill){
        billService.save(bill);
        Bill bill1=billService.getById(bill.getBillId());
        return Result.success("保存成功",bill);

    }
    @DeleteMapping("/{bill_id}")
    public Result delete(@PathVariable("bill_id") Integer bill_id){
        boolean b=billService.removeById(bill_id);
        if(b){
            return Result.success("删除成功");
        }
        else{
            return Result.fail(201,"删除失败");
        }
    }
    @PutMapping
    public Result edit(@RequestBody Bill bill){
        boolean b=billService.saveOrUpdate(bill);
        Bill bill1=billService.getById(bill.getBillId());
        if(b){
            return Result.success("修改成功");
        }
        else {
            return Result.fail(201,"修改失败");
        }
    }
}








